package com.aizhixin.lab.task.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.aizhixin.lab.task.domain.QuestionAndStuAnswerDomain;
import com.aizhixin.lab.task.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.course.template.service.TemplateAssistService;
import com.aizhixin.lab.task.core.ExerciseType;
import com.aizhixin.lab.task.core.TaskInforCode;
import com.aizhixin.lab.task.domain.TaskFileDomain;
import com.aizhixin.lab.task.domain.TaskQuestionChoiceDomain;
import com.aizhixin.lab.task.domain.TaskQuestionDomain;
import com.aizhixin.lab.task.repository.ReleaseTaskQuestionRepository;
import com.aizhixin.lab.task.repository.TaskQuestionRepository;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Transactional
@Service
public class ReleaseTaskQuestionsService {
    @Autowired
    private ReleaseTaskQuestionRepository releaseQuestionRespository;
    //    @Autowired
//    private TaskService taskService;
//    @Autowired
//    private TaskFileService taskFileService;
//    @Autowired
//    private TaskQuestionJDBC taskQuestionJDBC;
    @Autowired
    private ReleaseTaskQuestionChoiceService releaseTaskQuestionChoiceService;

    public void save(List<ReleaseTaskQuestion> taskQuestions) {
        releaseQuestionRespository.save(taskQuestions);
    }
    public Integer findQuestionNum(String releaseTaskId){
       return releaseQuestionRespository.countByReleaseTaskIdAndDeleteFlag(releaseTaskId,DataValidity.VALID.getState());
    }
    public ReleaseTaskQuestion findReleaseTaskQuestion(String releaseTaskQuestionId) {
        return releaseQuestionRespository.findByIdAndDeleteFlag(releaseTaskQuestionId, DataValidity.VALID.getState());
    }

    List<ReleaseTaskQuestion> findByReleaseTaskIdOrderByOrderNumAsc(String releaseTaskId) {
        return releaseQuestionRespository.findByReleaseTaskIdAndDeleteFlagOrderByOrderNumAsc(releaseTaskId, DataValidity.VALID.getState());
    }

    List<ReleaseTaskQuestion> findByReleaseTaskIdAndQuestionTypeOrderByOrderNumAsc(String releaseTaskId,Integer QuestionType) {
        return releaseQuestionRespository.findByReleaseTaskIdAndQuestionTypeAndDeleteFlag(releaseTaskId, QuestionType,DataValidity.VALID.getState());
    }

    public QuestionAndStuAnswerDomain findQuestion(String releaseTaskQuestionId) {
        QuestionAndStuAnswerDomain questionAndStuAnswerDomain = new QuestionAndStuAnswerDomain();
        ReleaseTaskQuestion releaseTaskQuestion = findReleaseTaskQuestion(releaseTaskQuestionId);
        questionAndStuAnswerDomain.setQuestionContent(releaseTaskQuestion.getQuestionContent());
        questionAndStuAnswerDomain.setAnswer(releaseTaskQuestion.getAnswer());
        questionAndStuAnswerDomain.setQuestionType(releaseTaskQuestion.getQuestionType());
        if (questionAndStuAnswerDomain.getQuestionType() == 10 || questionAndStuAnswerDomain.getQuestionType() == 20 || questionAndStuAnswerDomain.getQuestionType() == 30) {
            List<ReleaseQuestionChoice> choiceList = releaseTaskQuestionChoiceService.getChoiceList(releaseTaskQuestionId);
            questionAndStuAnswerDomain.setQuestionChoiceList(choiceList);
        }

        return questionAndStuAnswerDomain;
    }
}
